package com.vito.oa.act.extend.action;


import java.text.MessageFormat;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.config.annotation.Reference;
import com.vito.base.authoriza.action.BaseAction;
import com.vito.base.authoriza.cache.SessionMap;
import com.vito.base.authoriza.model.UserModel;
import com.vito.base.model.ResMsg;
import com.vito.base.model.Result;
import com.vito.base.util.Validata;
import com.vito.oa.act.extend.service.OaActApprovalFormService;

@Controller
@RequestMapping("oa/act/approval/form")
public class OaActApprovalFormAction extends BaseAction{
	private static final Logger log = LoggerFactory.getLogger(OaActApprovalFormAction.class);
	@Reference
	private OaActApprovalFormService actApprovalFormService;
	@ResponseBody
	@RequestMapping(value = "/saveForm")
	public Result saveForm(String details) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			String deptId = user.getDeptParentId();
			return actApprovalFormService.saveForm(user.getUserId(), deptId, details);
		} catch (Exception e) {
			log.error("保存用户自定义表单异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
	}
	@ResponseBody
	@RequestMapping(value = "/deleteForm")
	public Result deleteForm(String form_id) {
		Result r = new Result();
		try {
			if(Validata.isNullOrEmpty(form_id)){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "表单编码"));
				return r;
			}
			return actApprovalFormService.deleteForm(form_id);
		} catch (Exception e) {
			log.error("保存用户自定义表单异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
	}
	@ResponseBody
	@RequestMapping(value = "/updateForm")
	public Result updateForm(String details) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			return actApprovalFormService.updateForm(user.getUserId(), details);
		} catch (Exception e) {
			log.error("保存用户自定义表单异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
	}
	@ResponseBody
	@RequestMapping(value = "/queryFormByPage")
	public Result queryFormByPage(@RequestParam Map<String, String> map) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			String deptId = user.getDeptParentId();
			map.put("dept_id", deptId);
			if(Validata.isNullOrEmpty(map.get("pageNo"))){
				map.put("pageNo", "1");
			}
			if(Validata.isNullOrEmpty(map.get("pageSize"))){
				map.put("pageSize", "15");
			}
			r.setData(actApprovalFormService.queryForm(map));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("查询用户自定义表单异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	@ResponseBody
	@RequestMapping(value = "/queryFormCols")
	public Result queryFormCols(String form_id) {
		Result r = new Result();
		try {
			UserModel user = SessionMap.getObj();
			if(null==user){
				r.setCode(Result.CODE_LOGIN_REQUIRE);
				r.setMsg(ResMsg.MSG_LOGIN_NEED);
				return r;
			}
			if(Validata.isNullOrEmpty(form_id)){
				r.setCode(Result.CODE_PARA_ERROR);
				r.setMsg(MessageFormat.format(ResMsg.MSG_DATA_NEED, "表单编码"));
				return r;
			}
			r.setData(actApprovalFormService.queryFormCols(form_id));
			r.setCode(Result.CODE_OK);
		} catch (Exception e) {
			log.error("保存用户自定义表单异常：",e);
			r.setCode(Result.CODE_ERROR);
			r.setMsg(ResMsg.EXECERR);
			return r;
		}
		return r;
	}
	
}